
//
// 120312 - AH - (Garage) Created.
// 120313 - AH - Made Serializable and toString().
// 120324 - AH - (Bybanen *) CE(ConversationElement).
// 120329 - AH - (Midtun) Removed properties conversationData, chatInput and
//               chatOutput: Parameters should be used.
// 120411 - AH - (Nesttun Legesenter *) Removed commented away code.
// 120727 - AH - (Midtun) Now extends ParametersSerializable and removed code
//               that now is in base class.
//

package org.soekrat.server.serialization;

import java.util.Map;

import org.soekrat.server.io.db.ConversationElement;


/**
 * <p>
 *   Describe a conversation (element).
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class ConversationElementSerializable
  extends ParametersSerializable
{
  private static final long serialVersionUID=5299959529061508547L;

  private Long id;
  
  private Long owner=null;
  
  private String name;
  
  /**
   * <p>
   *   Constructor for faking created
   *   {@code CE}.
   * </p>
   */
  public ConversationElementSerializable()
  { 
    this(Long.toString(System.currentTimeMillis())); 
  }
  
  /**
   * <p>
   *   Constructor-
   * </p>
   * @param ce Object restored from database {@code this} is to represents.
   */
  public ConversationElementSerializable(ConversationElement ce)
  {
    if (ce==null)
    {
      throw new NullPointerException("ce");
    }
    
    id=ce.id();
    owner=ce.ownerId();
    name=ce.getParameter("name");
    
    Map<String, String> params=ce.getParameterMap();
    for (Map.Entry<String, String> curr : params.entrySet())
    {
      parameter(curr.getKey(), curr.getValue());  
    }
    
    if (id!=null)    parameter("id", Long.toString(id));
    if (owner!=null) parameter("owner", Long.toString(owner));
    if (name!=null)  parameter("name", name);
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param id Id.
   */
  public ConversationElementSerializable(String id)
  {
    if (id==null)
    {
      throw new NullPointerException("id");
    }
    
    try
    {
      long idc=Long.parseLong(id);
      if (idc<0)
      {
        throw new IllegalArgumentException("id<0: "+idc);
      }
      
      this.id=idc;
    }
    catch (NumberFormatException nfx)
    {
      throw new IllegalArgumentException("could not parse id as long: "+id);
    }
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param id Id.
   */
  public ConversationElementSerializable(long id)
  {
    if (id<0)
    {
      throw new IllegalArgumentException("id<0: "+id);
    }
    
    this.id=id;
  }
  
  /**
   * <p>
   *   Gets id that identifies conversation {@code this} represents.
   * </p>
   * @return Id.
   */
  public long id(){ return id; }
  
  /**
   * <p>
   *   Gets id of owner.
   * </p>
   * @return Owner's id or {@code null} if not sat. 
   */
  public Long owner(){ return this.owner; }
  
  /**
   * <p>
   *   Sets id that identifies owner of object {@code this} represents.
   * </p>
   * @param id Id to set.
   * @return {@code this}.
   * @throws IllegalArgumentException If {@code id<0}.
   */
  public ConversationElementSerializable owner(long id)
  {
    if (id<0)
    {
      throw new IllegalArgumentException("id<0 : "+id);
    }
    
    this.id=id;
    
    return this;
  }
  
  /**
   * <p>
   *   Sets name of conversation.
   * </p>
   * @param name Name.
   * @return {@code this}.
   */
  public ConversationElementSerializable name(String name)
  { 
    this.name=name; 
    return this; 
  }
  
  /**
   * <p>
   *   Gets name of conversation.
   * </p>
   * @return Name or {@code null}.
   */
  public String name(){ return name; }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    
    boolean first=true;
    if (id!=null)
    { 
      sb.append("id=").append(id); 
      first=false;
    }
    if (name!=null)
    { 
      if (!first) sb.append(", ");
      sb.append("name=").append(name);
      first=false;
    }
    
    return sb.toString();
  }
  
  /**
   * <p>
   *   The empty array shared.
   * </p>
   */
  public static final ConversationElementSerializable[] EMPTY_ARRAY=
    new ConversationElementSerializable[0];
  
}
